from PyQt5.QtNetwork import QHostAddress, QUdpSocket
from PyQt5.QtCore import QThread, QObject
import typing

class XuanWuUDPControler(QThread):
    def __init__(self, parent: typing.Optional[QObject]) -> None:
        super().__init__(parent=parent)
        self.__udp_socket_ = QUdpSocket(self)
        self.__payload_ = ''.encode('utf-8')
        self.__bindIp = '127.0.0.1'
        self.__bindPort = 8888

    def CommandRequest(self, command: str, str_ip: str, str_port: str) -> None:
        self.__payload_ = command.encode('utf-8')
        self.__bindIp = str_ip
        self.__bindPort = int(str_port)
        self.start()
    
    def run(self) -> None:
        # return super().run()
        self.__udp_socket_.writeDatagram(self.__payload_, QHostAddress(self.__bindIp), self.__bindPort)